<template>
  <div>
    <tableComponents :pagination="{ currentPage, pageSize, total: list.all_num }" @handleCurrentChange="handleCurrentChange"
        @handleSizeChange="handleSizeChange" @sendUserSearch="receive_search" :searchData="userSearchData" :isHaveExport="false" @toAdd="add_data">
      <template v-slot:table>
        <el-table ref="multipleTable" :data="list.data" stripe v-loading="loading" header-row-class-name="head_color" tooltip-effect="dark"
            style="width: 99.8%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" class="tc" width="35"></el-table-column>
          <!-- <el-table-column prop="sort" sortable :sort-method="sorta" label="排序" min-width="100">
            <template slot-scope="scope">
              <el-input @change="sortNew(scope.$index, scope.row)" v-model.number="scope.row.sort" autocomplete="off" size="small"></el-input>
            </template>
          </el-table-column> -->

          <el-table-column label="层级" min-width="50">
            <template slot-scope="scope">{{ scope.row.level }}</template>
          </el-table-column>

          <el-table-column label="标题" min-width="80">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>
          <el-table-column label="地址" min-width="220">
            <template slot-scope="scope">
              {{ scope.row.merger_name || "----" }}
              <!-- <div class="tab_div">
                <div>
                  <span>地址：</span>
                  <span>{{ scope.row.merger_name || "----" }}</span>
                </div>
              </div> -->
            </template>
          </el-table-column>
          <el-table-column label="信息" min-width="160">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>短标题：</span>
                  <span>{{ scope.row.short_name || "----" }}</span>
                </div>
                <div>
                  <span>短标题拼音：</span>
                  <span>{{ scope.row.pinyin || "----" }}</span>
                </div>
              </div>
            </template>
          </el-table-column>

          <el-table-column label="经纬度" min-width="120">
            <template slot-scope="scope">
              <div class="tab_div">
                <div>
                  <span>经度：</span>
                  <span>{{ scope.row.lng || "----" }}</span>
                </div>
                <div>
                  <span>纬度：</span>
                  <span>{{ scope.row.lat || "----" }}</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="添加时间" min-width="220">
            <template slot-scope="scope">
              <div class="user_table">
                {{ scope.row.created_time | getTimeFormat }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" min-width="120">
            <template slot-scope="scope">
              <el-row class="myblank">
                <el-button :loading="$store.state.isLoading" size="mini" @click="editbtn(scope.row)" type="primary">编辑</el-button>
                <el-button :loading="$store.state.isLoading" size="mini" type="danger" @click="deletebtn(scope.row.id)">删除</el-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <!-- <a class="aBtn aBtn_1 aBtn_lan2" @click="deleteAll">删除</a> -->
      </template>
    </tableComponents>

    <el-dialog :close-on-click-modal="false" title="编辑" class="tl size_13" width="800px" :visible.sync="dialogFormVisible"
        v-if="dialogFormVisible == true">
      <el-form :model="live_single" ref="form" label-width="140px">
        <el-form-item label="所属上级：" v-if="is_edit == false">
          <el-col :span="16">
            <city-cascader :is_id="true" :level="3" :checkStrictly="true" @change="handleItemChange($event, it)" />
          </el-col>
        </el-form-item>
        <el-form-item label="标题：">
          <el-col :span="16">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="live_single.title" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="短标题：">
          <el-col :span="16">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="live_single.short_name" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="地址信息：" v-if="is_edit == true">
          <el-col :span="16">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="live_single.merger_name" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="短标题拼音：">
          <el-col :span="16">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="live_single.pinyin" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="经度：">
          <el-col :span="16">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="live_single.lng" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="纬度：">
          <el-col :span="16">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="live_single.lat" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="地区编码：">
          <el-col :span="16">
            <el-input type="text" :rows="6" clearable placeholder="请输入" v-model="live_single.zip_code" autocomplete="off"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label-width="140px" >
          <el-button :loading="$store.state.isLoading" type="primary" @click="editlive(live_single)">编 辑</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { addressSearchData } from "@/assets/js/searchData/searchData"; //引入搜索的数据
import cityCascader from "@/components/currency/cityCascader/cityCascader";
export default {
  data () {
    return {
      userSearchData: addressSearchData,
      is_edit: false,
      dialogFormVisible: false,
      live_single: {},
      current: "",
      loading: false,
      list: [],
      allPage: 0,
      allNum: 0,
      pageSize: 10,
      currentPage: 1,
      searchAll: {},
      row: {},
      multipleSelection: [],
      showCity: true
    };
  },
  components: {
    cityCascader
  },
  methods: {
    handleItemChange (val, item) {
      this.$set(this.live_single, "id", val[val.length - 1]);
    },
    sorta (a, b) {
      return b - a;
    },
    sortNew (index, row) {
      var params = {};
      params = row;
      params.id = row.id;
      params.sort = row.sort;
      this.$api.getPage.edit_addresslist(params).then(res => {
        if (res.data.code == 200 && res.data.result) {
          return;
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
        }
      });
    },
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },
    deleteAll () {
      //批量删除
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思"
        }).then(willDelete => {
          if (willDelete.value) {
            this.$api.getPage.del_addresslist({ id: str }).then(res => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这条信息。", "success");
                this.getNewsAll(this.searchAll);
              } else {
                this.$swal("删除失败！", res.data.result, "success");
                this.getNewsAll(this.searchAll);
              }
            });
          }
        });
      }
    },
    deletebtn (id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          this.$api.getPage.del_addresslist({ id: id }).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getNewsAll(this.searchAll);
            } else {
              this.$swal("删除失败！", res.data.result, "success");
              this.getNewsAll(this.searchAll);
            }
          });
        }
      });
    },
    add_data () {
      this.live_single = {};
      this.is_edit = false;
      this.dialogFormVisible = true;
    },

    getIs (val, row) {
      this.is_edit = true;
      var params = {};
      params = row;
      params.id = row.id;
      params.show = val;
      this.editlive(params);
    },

    editlive (item) {
      var params = {};
      if (this.is_edit == false) {
        params = item;
        this.$api.getPage.add_addresslist(params).then(res => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.$fnc.alertSuccess(res.data.result);
            this.getNewsAll(this.searchAll);
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      } else {
        params = item;
        this.$api.getPage.edit_addresslist(params).then(res => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.$fnc.alertSuccess(res.data.result);
            this.getNewsAll(this.searchAll);
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      }
    },
    editbtn (item) {
      this.is_edit = true;
      this.live_single = item;
      this.dialogFormVisible = true;
    },

    receive_search (obj) {
      this.searchAll = obj;
      this.getNewsAll(obj);
    },
    getNewsAll (data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;
      this.$api.getPage.get_addresslist(params).then(res => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    handleSizeChange (val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;
      this.getNewsAll(params);
      this.pageSize = val;
    },
    handleCurrentChange (val) {
      this.currentPage = val;
      var params = this.searchAll;
      params.page = val;
      params.page_size = this.pageSize;
      this.getNewsAll(params);
    },

    reload () {
      this.getNewsAll(this.searchAll);
    }
  },
  beforeCreate () { },
  created () {
    this.getNewsAll();
  },
  computed: {}
};
</script>

<style lang="less" scoped>
.myblank button {
  margin: 5px 5px 0 0;
}
</style>
